﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Web;
using System.Web.Security;

using SEiBuyAgency.User;
using SEiBuyAgency.Factory;
using SEiBuyAgency.Template;

namespace SEiBuyAgency
{
    public partial class MainForm : Form,IUserActionObserver
    {
        OleDbConnection aConnection;
        CUser user;
        private CUser creatUser(ICreator x)
        {
            return x.FactoryMethod(this);
        }

        private void ShowFormText(CTemplate atemp)
        {
            this.Text= atemp.Show();
        }
       

        public MainForm()
        {
            InitializeComponent();
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {
            
        }
        string conn=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
        private void MainForm_Load(object sender, EventArgs e)
        {
            radioButton9_CheckedChanged(sender, e);
            //conn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

            this.FormBorderStyle = FormBorderStyle.Fixed3D;
            this.tabControl1.Visible = false;

            //从seller处获取数据更新数据库
            WebReference.SrvCSharpKnife _seller = new WebReference.SrvCSharpKnife();
            WebReference.ExchangeGoods[] _egoods;
            aConnection = new OleDbConnection(conn);

            _egoods = _seller.listAllProducts("伐木甲乙丙");
            int num = _egoods.Length;

            for (int i = 0; i < num; i++)
            {
                //向Supplier表中加入未知的SupplierID
                string sql = "select * from [Supplier] where SupplierID = " + "'" + _egoods[i].Supplier + "'";

                OleDbDataAdapter _da = new OleDbDataAdapter(sql, aConnection);
                DataSet _ds = new DataSet();
                _da.Fill(_ds);

                if (_ds.Tables[0].Rows.Count == 0)
                {
                    sql = "insert into [Supplier] (SupplierID) values ('" + _egoods[i].Supplier + "')";
                    _da = new OleDbDataAdapter(sql, aConnection);
                    _ds = new DataSet();
                    _da.Fill(_ds);
                    //MessageBox.Show("数据库中没有这条供应商记录");
                }

                //向GoodsCategory表中加入未知的CategoryName
                sql = "select * from [GoodsCategory] where CategoryName = " + "'" + _egoods[i].GoodsCate.CategoryName + "'";

                OleDbDataAdapter da_1 = new OleDbDataAdapter(sql, aConnection);
                DataSet ds_1 = new DataSet();
                da_1.Fill(ds_1);

                if (ds_1.Tables[0].Rows.Count == 0)
                {
                    sql = "INSERT INTO [GoodsCategory] (CategoryName) values ('" + _egoods[i].GoodsCate.CategoryName + "')";
                    da_1 = new OleDbDataAdapter(sql, aConnection);
                    ds_1 = new DataSet();
                    da_1.Fill(ds_1);
                    //MessageBox.Show("数据库中没有这条产品类别记录");
                }

                //向Goods表中加入未知的商品信息
                sql = "select * from [Goods] where GoodsID = " + "'" + _egoods[i].GoodsID + "'";

                OleDbDataAdapter da_2 = new OleDbDataAdapter(sql, aConnection);
                DataSet ds_2 = new DataSet();
                da_2.Fill(ds_2);

                if (ds_2.Tables[0].Rows.Count == 0)
                {
                    string sql_gc = "select ID from [GoodsCategory] where CategoryName = " + "'" + _egoods[i].GoodsCate.CategoryName + "'";
                    OleDbDataAdapter da_gc = new OleDbDataAdapter(sql_gc, aConnection);
                    DataSet ds_gc = new DataSet();
                    da_gc.Fill(ds_gc);
                    int gc = Convert.ToInt32(ds_gc.Tables[0].Rows[0].ItemArray[0]);

                    String sql_sp = "select ID from [Supplier] where SupplierID = " + "'" + _egoods[i].Supplier + "'";
                    OleDbDataAdapter da_sp = new OleDbDataAdapter(sql_sp, aConnection);
                    DataSet ds_sp = new DataSet();
                    da_sp.Fill(ds_sp);
                    int sp = Convert.ToInt32(ds_sp.Tables[0].Rows[0].ItemArray[0]);


                    sql = "insert into [Goods] (GoodsID,GoodsName,GoodsAmount,GoodsPrice,GoodsCate,GoodsSupplier,SendTime) values ('" + _egoods[i].GoodsID + "','" + _egoods[i].GoodsName + "','" + _egoods[i].GoodsAmount + "','" + _egoods[i].GoodsPrice + "','" + gc + "','" + sp + "','" + _egoods[i].SendTime + "')";
                    da_2 = new OleDbDataAdapter(sql, aConnection);
                    ds_2 = new DataSet();
                    da_2.Fill(ds_2);
                    //MessageBox.Show("数据库中没有这条产品信息记录");
                }
            }


            //从本地数据库中读取信息
            string sqlt = "select GoodsID as 商品ID,GoodsName as 商品名称,GoodsAmount as 库存数量,GoodsPrice as 商品价格,CategoryName as 种类 ,sendTime as 发货时间 from goodsinfo";
            try
            {
                aConnection.Open();
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Close();
            }
            OleDbDataAdapter da = new OleDbDataAdapter(sqlt, aConnection);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];

            loadLevelData();

            string sqlt3 = "select ID,CategoryName from [GoodsCategory] ";
            OleDbDataAdapter da3 = new OleDbDataAdapter(sqlt3, aConnection);
            DataSet ds3 = new DataSet();
            da3.Fill(ds3);
            for (int i = 0; i < ds3.Tables[0].Rows.Count;i++ )
            {
                comboBox1.Items.Add(ds3.Tables[0].Rows[i].ItemArray[1]);
            }
            aConnection.Close();




            this.tabControl2.Visible = false;



        }
        private void loadLevelData()
        {
            //conn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
            aConnection = new OleDbConnection(conn);
            string sql = "select id as 等级ID,levelName as 等级名称,discount as 折扣 from userlevel";
            OleDbDataAdapter da = new OleDbDataAdapter(sql, aConnection);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView3.DataSource = ds.Tables[0];
            aConnection.Close();
        }
        private void loadOrderData()
        {
            //conn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
            aConnection = new OleDbConnection(conn);
            string sql = "select GoodsID  as 商品ID, GoodsName as 商品名称, OrderNumber as 购买数量, OrderMoney as 消费金额 ,levelName as 折扣 from userorder,goodsinfo,userlevel where userorder.userlevel=userlevel.id and userorder.OrderGoodsID=goodsinfo.GoodsID and orderUserID='" + this.textBox9.Text + "'";
            OleDbDataAdapter da = new OleDbDataAdapter(sql, aConnection);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView2.DataSource = ds.Tables[0];
            aConnection.Close();
        }
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.tabControl1.SelectedIndex)
            {
                case 3:

                    break;
                case 1:
                    aConnection = new OleDbConnection(conn);
                    string sqlt = "select GoodsID  as 商品ID, GoodsName as 商品名称, OrderNumber as 购买数量, OrderMoney as 消费金额 ,levelName as 折扣 from userorder,goodsinfo,userlevel where userorder.userlevel=userlevel.id and userorder.OrderGoodsID=goodsinfo.GoodsID and orderUserID='" + this.textBox9.Text + "'";
                    try
                    {
                        aConnection.Open();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        this.Close();
                    }
                    OleDbDataAdapter da = new OleDbDataAdapter(sqlt, aConnection);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    dataGridView2.DataSource = ds.Tables[0];


                    aConnection.Close();
                    break;
                case 2:

                    break;
                default:
                    break;
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sqlt = "select GoodsID as 商品ID,GoodsName as 商品名称,GoodsAmount as 库存数量,GoodsPrice as 商品价格,CategoryName as 种类 ,sendTime as 发货时间 from goodsinfo where 1=1 ";
            if (textBox12.Text!="")
            {
                sqlt += " and GoodsName like '%" + textBox12.Text + "%'";
            }

            if (dateTimePicker1.Text != "" && dateTimePicker1.Checked)
            {
                sqlt += " and sendTime = '" + dateTimePicker1.Text + "'";
            }

            if (comboBox1.Text!="")
            {
                sqlt += " and CategoryName = '" + comboBox1.Text + "'";
            }
            //if (this.radioButton1.Checked)
            //    sqlt = "select GoodsID as 商品ID,GoodsName as 商品名称,GoodsAmount as 库存数量,GoodsPrice as 商品价格,CategoryName as 种类 ,sendTime as 发货时间 from goodsinfo";
            //if (this.radioButton2.Checked)
            //    sqlt = "select GoodsID as 商品ID,GoodsName as 商品名称,GoodsAmount as 库存数量,GoodsPrice as 商品价格,CategoryName as 种类 ,sendTime as 发货时间 from goodsinfo order by GoodsCategory.CategoryName";
            //if (this.radioButton3.Checked)
            //    sqlt = "select GoodsID as 商品ID,GoodsName as 商品名称,GoodsAmount as 库存数量,GoodsPrice as 商品价格,CategoryName as 种类 ,sendTime as 发货时间 from goodsinfo order by Goods.GoodsName";
            //if (this.radioButton4.Checked)
            //    sqlt = "select GoodsID as 商品ID,GoodsName as 商品名称,GoodsAmount as 库存数量,GoodsPrice as 商品价格,CategoryName as 种类 ,sendTime as 发货时间 from goodsinfo order by Goods.GoodsPrice";
            //if (this.radioButton6.Checked)
            //    sqlt = "select GoodsID as 商品ID,GoodsName as 商品名称,GoodsAmount as 库存数量,GoodsPrice as 商品价格,CategoryName as 种类 ,sendTime as 发货时间 from goodsinfo order by Goods.GoodsAmount";
            //if (this.radioButton7.Checked)
            //    sqlt = "select GoodsID as 商品ID,GoodsName as 商品名称,GoodsAmount as 库存数量,GoodsPrice as 商品价格,CategoryName as 种类 ,sendTime as 发货时间 from goodsinfo";

            aConnection.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(sqlt, aConnection);
            DataSet ds = new DataSet();
           da.Fill(ds);
           dataGridView1.DataSource = ds.Tables[0];
           aConnection.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.textBox2.ReadOnly = false;
            this.textBox2.BackColor = Color.AliceBlue;
            this.textBox2.BorderStyle = BorderStyle.Fixed3D;
            this.textBox3.ReadOnly = false;
            this.textBox3.BackColor = Color.AliceBlue;
            this.textBox3.BorderStyle = BorderStyle.Fixed3D;
            this.textBox4.BackColor = Color.AliceBlue;
            this.textBox4.Visible = true;
            this.label4.Visible = true;
            this.button3.Visible = false;
            this.button4.Visible = true;

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (this.textBox2.Text != textBox4.Text)
                MessageBox.Show("密码不一致！");
            else
            {
                user.ChancgePwd(this.textBox1.Text, textBox2.Text,textBox3.Text);

            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            RegForm f = new RegForm();
            f.ShowDialog();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            bool t = false;
            string sqlt = @"SELECT * FROM [User] where userid = '" + this.textBox9.Text + "' and userpwd = '" + this.textBox8.Text + "';";
            aConnection.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(sqlt, aConnection);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count == 1)
            {
                t = true;
                int isadm = (int)ds.Tables[0].Rows[0].ItemArray[4];
                if (isadm == 1)
                    this.tabControl2.Visible = true;
                aConnection.Close();

                //验证用户
                if (t)
                {
                    //this.radioButton7.Visible = false;
                    this.groupBox7.Visible = false;
                    this.tabControl1.Dock = DockStyle.Fill;
                    this.tabControl1.Visible = true;
                    this.SetBounds(this.Location.X, this.Location.Y, this.Size.Width + 1, this.Size.Height + 1);

                    int x = (int)ds.Tables[0].Rows[0].ItemArray[5];
                    switch (x)
                    {
                        case 1:
                            user = this.creatUser(new UserL1Creator());
                            user.Login(this.textBox9.Text);
                            ShowFormText(new CFormText1(this.textBox9.Text));
                            break;
                        case 2:
                            user = this.creatUser(new UserL2Creator());
                            user.Login(this.textBox9.Text);
                            ShowFormText(new CFormText2(this.textBox9.Text));
                            break;
                        case 3:
                            user = this.creatUser(new UserL3Creator());
                            user.Login(this.textBox9.Text);
                            ShowFormText(new CFormText3(this.textBox9.Text));
                            break;
                        case 4:
                            user = this.creatUser(new UserL4Creator());
                            user.Login(this.textBox9.Text);
                            ShowFormText(new CFormText4(this.textBox9.Text));
                            break;
                        case 5:
                            user = this.creatUser(new UserL5Creator());
                            user.Login(this.textBox9.Text);
                            ShowFormText(new CFormText5(this.textBox9.Text));
                            break;
                    }
                    user.Id = this.textBox9.Text;

                    string sqlt3 = "select useremail from [User] where userid='" + user.Id + "'";
                    da = new OleDbDataAdapter(sqlt3, aConnection);
                    ds.Clear();
                    da.Fill(ds);
                    if (ds.Tables[0].Rows.Count == 1)
                    {
                        textBox3.Text = (string)ds.Tables[0].Rows[0].ItemArray[3];
                    }

                }
                else
                {
                    this.lbErrNote.Text = "用户名或密码有误！";
                    aConnection.Close();
                }
            }
            else
            {
                this.lbErrNote.Text = "用户名或密码有误！";
                aConnection.Close();
            }
        }
        private void button8_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void groupBox7_Enter(object sender, EventArgs e)
        {

        }

        #region IUserActionObserver 成员

        public void Completely(TAction action, bool suc)
        {
            string str = "";
            if (suc)
            {
                switch (action)
                {
                    case TAction.EBuy:
                        str = "购买成功！";
                        break;
                    case TAction.ELogin:
                        this.textBox1.Text = textBox9.Text;
                        this.textBox2.Text = this.textBox8.Text;
                        this.textBox4.Text = this.textBox8.Text;
                        break;
                    case TAction.Elogout:
                        break;
                    case TAction.EChangePwd:
                        str = "修改成功！";
                        this.textBox2.ReadOnly = true;
                        this.textBox2.BackColor = Color.White;
                        this.textBox2.BorderStyle = BorderStyle.None;
                        this.textBox3.ReadOnly = true;
                        this.textBox3.BackColor = Color.White;
                        this.textBox3.BorderStyle = BorderStyle.None;
                        this.textBox4.BackColor = Color.White;
                        this.textBox4.Visible = false;
                        this.label4.Visible = false;
                        this.button3.Visible = true;
                        this.button4.Visible = false;

                        break;
                    case TAction.EFeedBack:
                        str = "反馈成功！";
                        break;
                }
            }
            else
            {
                switch (action)
                {
                    case TAction.EBuy:
                        str = "购买失败！";
                        break;
                    case TAction.ELogin:
                    case TAction.Elogout:
                        break;
                    case TAction.EChangePwd:
                        str = "密码修改失败！";
                        break;
                    case TAction.EFeedBack:
                        str = "反馈失败！";
                        break;
                }
            }
            if (str.Length != 0)
                MessageBox.Show(str);
        }

        #endregion

        private void textBox9_MouseCaptureChanged(object sender, EventArgs e)
        {
            this.lbErrNote.Text = "";
        }

        private void textBox8_MouseCaptureChanged(object sender, EventArgs e)
        {
            this.lbErrNote.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OleDbConnection aConnection = new OleDbConnection(conn);

            int userlevel ;

            float curUserLevelDiscount;//当前用户等级折扣

            float goodsPrice;//折前金额

            double orderCount;//消费总金额

            //DataSet ds = new DataSet();

            string sql = "";

            if((textBox5.Text == "")||(textBox6.Text == ""))
            {
                MessageBox.Show("购买失败，您没有输入所要购买的产品或者购买数量非法！");
                return;
            }

            //查看库存是否够用,减少相应库存
            if (rmFromGoods(textBox5.Text, int.Parse(textBox6.Text)))
            {
                //查找用户等级对应折扣，商品金额
                DataSet ds = new DataSet();
                new OleDbDataAdapter("select UserLevel.id,discount from UserLevel,[USER] where User.UserLevel=UserLevel.id and User.UserID='" + user.Id + "'", aConnection).Fill(ds);
                userlevel = (int)ds.Tables[0].Rows[0].ItemArray[0];
                curUserLevelDiscount = (int)ds.Tables[0].Rows[0].ItemArray[1];

                ds = new DataSet();
                new OleDbDataAdapter("select goodsprice from goods where GoodsID='" + textBox5.Text + "'", aConnection).Fill(ds);
                goodsPrice = (float)ds.Tables[0].Rows[0].ItemArray[0];


                //userorder 增加购买记录 （消费金额=用户等级折扣/100*实际金额）

                WebReference.SrvCSharpKnife seller = new SEiBuyAgency.WebReference.SrvCSharpKnife();
                //seller.OrderProducts("伐木甲乙丙", textBox5.Text, Convert.ToInt32(textBox6.Text));

                int result = -1;
                try
                {
                    aConnection.Open();
                    OleDbCommand cmd = new OleDbCommand("insert into UserOrder(orderuserid,ordergoodsid,ordernumber,ordermoney,userlevel) values('" + user.Id + "','" + textBox5.Text + "'," + textBox6.Text + "," + (curUserLevelDiscount / 100 * goodsPrice * float.Parse(textBox6.Text)) + "," + userlevel + ")", aConnection);
                    result = cmd.ExecuteNonQuery();
                    aConnection.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    result = 0;
                }
                loadOrderData();
                
                
                //user 更新用户等级 （比对消费金额与等级范围，等级范围暂时写死）
                ds = new DataSet();
                new OleDbDataAdapter("select sum(ordermoney) from UserOrder where orderuserid='" + user.Id + "'", aConnection).Fill(ds);
                orderCount = (double)ds.Tables[0].Rows[0].ItemArray[0];
                if (orderCount > 10 && orderCount < 100)
                {
                    //银牌
                    sql = @"UPDATE [User] SET userlevel=2 WHERE userid='" + user.Id + @"';";
                }
                else if (orderCount > 100 && orderCount < 1000)
                {
                    //金牌
                    sql = @"UPDATE [User] SET userlevel=3 WHERE userid='" + user.Id + @"';";
                }
                else if (orderCount > 1000 && orderCount < 10000)
                {
                    //钻石
                    sql = @"UPDATE [User] SET userlevel=4 WHERE userid='" + user.Id + @"';";
                }
                else if (orderCount > 10000)
                {
                    //VIP
                    sql = @"UPDATE [User] SET userlevel=5 WHERE userid='" + user.Id + @"';";
                }
                if (sql != "")
                {
                    try
                    {
                        aConnection.Open();
                        OleDbCommand cmd = new OleDbCommand(sql, aConnection);
                        result = cmd.ExecuteNonQuery();
                        aConnection.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        result = 0;
                    }
                }
                aConnection.Close();
            }
            else
                this.Completely(TAction.EBuy, false);
            
            
            ////values('" + userId + @"','" + goodsId + @"'," + goodsnumber.ToString() + @");";
            //int result = -1;
            //try
            //{
            //    aConnection.Open();
            //    OleDbCommand cmd = new OleDbCommand(sqlt, aConnection);
            //    result = cmd.ExecuteNonQuery();
            //    aConnection.Close();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //    result = 0;
            //}
            //MessageBox.Show("ok");
            //user.Buy(this.textBox5.Text, Int32.Parse(this.textBox6.Text));
            button1_Click(sender,e);
            textBox5.Text = "";
            textBox6.Text = "";
        }

    

        private void tabPage5_Click(object sender, EventArgs e)
        {

        }

        private void button10_Click(object sender, EventArgs e)
        {

            //conn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

            string sqlt = "update userlevel set discount=" + this.textBox11.Text + " where id=" + this.textBox10.Text+ ";";
                //"update userlevel set discount=" + int.Parse(this.textBox10.Text) + " where id="+long.Parse(this.textBox11.Text)+";";
            try
            {
                aConnection.Open();
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Close();
            }
            int result = 0;
            OleDbCommand cmd = new OleDbCommand(sqlt, aConnection);
            try
            {
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
 
            }
           if (result == 1)
               MessageBox.Show("设置成功");
           else
               MessageBox.Show("设置失败，请保证输入有效数字！");




           string sqlt2 = "select id as 等级ID,levelName as 等级名称,discount as 折扣 from userlevel";
            OleDbDataAdapter da2 = new OleDbDataAdapter(sqlt2, aConnection);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2);
            dataGridView3.DataSource = ds2.Tables[0];
            
            
            aConnection.Close();


        }

        private void radioButton9_CheckedChanged(object sender, EventArgs e)
        {
            load_data("SELECT SupplierID as 生产厂家,count(GoodsName) as 商品数量 FROM goods , Supplier where goods.GoodsSupplier=Supplier.id group by SupplierID;", "生产厂家", "商品数量");
        }

        private void radioButton8_CheckedChanged(object sender, EventArgs e)
        {
            load_data("SELECT GoodsName as 商品名称,sum(GoodsPrice) as 商品价格 FROM goods group by GoodsName,GoodsPrice;", "商品名称", "商品价格");
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            load_data("SELECT GoodsName as 商品名称,sendTIME as 发货时间 FROM goods group by sendTIME,GoodsName;", "商品名称", "发货时间");
        }

        private void load_data(string sql, string xFields, string yFields)
        {
            string myConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
            // Create a database connection object using the connection string    
            OleDbConnection myConnection = new OleDbConnection(myConnectionString);
            try
            {
                myConnection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Close();
            }
            OleDbDataAdapter da = new OleDbDataAdapter(sql, myConnection);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView4.DataSource = ds.Tables[0];
            dataGridView4.AllowUserToAddRows = false;
            dataGridView4.AllowUserToDeleteRows = false;
            dataGridView4.ReadOnly = true;
            chart1.Series["Series1"].Points.DataBindXY(ds.CreateDataReader(), xFields, ds.CreateDataReader(), yFields);
            myConnection.Close();
        }

        private bool rmFromGoods(string id, int amount)
        {
            OleDbConnection aConnection = new OleDbConnection(conn);

            string sqlt = "update goods set goodsamount=goodsamount-" + amount + " where goodsID='" + id + "'";
            int result = -1;
            try
            {
                aConnection.Open();
                OleDbCommand cmd = new OleDbCommand(sqlt, aConnection);
                result = cmd.ExecuteNonQuery();
                aConnection.Close();
            }
            catch (Exception)
            {
                result = 0;
                aConnection.Close();
            }

            if (result == 1)
                return true;
            else
                return false;
        }
    }
   

}
